/* eslint-disable eqeqeq */
import actionInfo from '../action'
import dayjs from 'dayjs'
import { partaAdd, partaQuery } from '../service/partaService'

function partaInfoReducer(state = { partaList: [] }, action) {
    switch (action.type) {
        case actionInfo.parta.ADD:
            return { partaList: [...state.partaList, action.payload] }
        case actionInfo.parta.QUERY:
            return { partaList: action.payload }
        default:
            return state
    }
}
export const asyncPartaQuery = (params) => async (dispatch) => {
    let { data } = await partaQuery(params);//做异步请求
    let partalist = data.msg.map(item => ({ ...item, date: dayjs(item.date).format('YYYY-MM-DD') }))
    dispatch(partaInfoQuery(partalist));//
}
const partaInfoQuery = (payload) => {
    return {
        type: actionInfo.parta.QUERY,
        payload
    }
}
export const asyncPartaAdd = (params) => async (dispatch) => {
    let { data } = await partaAdd(params);//做异步请求
    if (data.code == 1) {
        let partalist = data.msg.map(item => ({ ...item, date: dayjs(item.date).format('YYYY-MM-DD') }))
        dispatch(partaInfoAdd(...partalist));
    }
}
const partaInfoAdd = (payload) => {
    return {
        type: actionInfo.parta.ADD,
        payload
    }
}
export default partaInfoReducer